package graph.weight;

import linear.Queue;

/**
 * 加权有向图
 */
public class DirectedEdgeWeightedGraph {
    /**
     * 顶点数量
     */
    private final int V;
    /**
     * 边数量
     */
    private int E;
    /**
     * 邻接表
     */
    private final Queue<DirectedEdge>[] adj;


    public DirectedEdgeWeightedGraph(int V) {
        // 初始化
        this.V = V;
        E = 0;
        adj = new Queue[V];
        for (int i = 0; i < V; i++) {
            adj[i] = new Queue<>();
        }

    }

    public int V() {
        return V;
    }

    public int E() {
        return E;
    }

    public Queue<DirectedEdge> adj(int v) {
        return adj[v];
    }

    public void addEdge(DirectedEdge e) {
        int v = e.from();
        adj[v].enqueue(e);
        E++;
    }

    /**
     * 有向图 邻接表中的边不会重复 故 遍历邻接表即可
     */
    public Queue<DirectedEdge> edges() {
        Queue<DirectedEdge> allEdge = new Queue<>();
        for (int v = 0; v < this.V; v++) {

            for (DirectedEdge e : adj(v)) {
                allEdge.enqueue(e);
            }
        }
        return allEdge;
    }

}
